package com.osmino.contacts.view.list;


import java.util.ArrayList;
import java.util.List;

import com.osmino.contacts.model.contacts.IContact;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public final class ContactListAdapter extends BaseAdapter {

	private final Activity mActivity;
	private List<IContact> mListContact;

	public ContactListAdapter(Activity activity) {
		mActivity = activity;
		mListContact = new ArrayList<IContact>();
	}

	@Override
	public int getCount() {
		return mListContact.size();
	}

	@Override
	public Object getItem(int position) {
		return mListContact.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parentView) {
		ContactItem item = null;
		if (convertView == null) {
			item = new ContactItem(mActivity, mListContact.get(position), position);
		} else {
			item = (ContactItem) convertView;
			item.clearInfo();
			item.setInfo(mListContact.get(position));
		}
		return item;
	}
	
	public void refreshList(List<IContact> listContactInfo) {
		mListContact.clear();
		mListContact.addAll(listContactInfo);
		notifyDataSetChanged();
	}
}
